using System;
using Headspring.Training.Core.Services;

namespace Headspring.Training.Core.Model.StateCommands
{
	public class AssignedToDraftCommand : StateCommandBase
	{
		private readonly IEmailService _emailService;
		private readonly ICalendar _calendar;

		public AssignedToDraftCommand(WorkOrder workOrder, Employee currentUser,
		                              IEmailService emailService, ICalendar calendar)
			: base(workOrder, currentUser)
		{
			_emailService = emailService;
			_calendar = calendar;
		}

		public override string TransitionVerbPresentTense
		{
			get { return "Reject"; }
		}

		public override string TransitionVerbPastTense
		{
			get { return "Rejected"; }
		}

		public override WorkOrderStatus GetBeginStatus()
		{
			return WorkOrderStatus.Assigned;
		}

		protected override WorkOrderStatus GetEndStatus()
		{
			return WorkOrderStatus.Draft;
		}

		protected override bool userCanExecute(Employee currentUser)
		{
			return (_workOrder.Assignee == currentUser);
		}

		protected override void postExecute(IStateCommandVisitor commandVisitor)
		{
			Employee creator = _workOrder.Creator;
			if (_currentUser.ReportsTo(creator))
			{
				DayOfWeek dayOfWeek = _calendar.GetCurrentTime().DayOfWeek;
				_emailService.SendMessage(creator.GetRejectEmail(dayOfWeek),
				                          "Not gonna do it.");
			}

			commandVisitor.EditWorkOrder(_workOrder);
		}
	}
}